import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class Test7 {
//新建会话
    public static void main(String[] args) throws Exception {

        // 设置 Appium 客户端和系统参数
        DesiredCapabilities caps = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "127.0.0.1:62001");
        caps.setCapability("platformName", "Android");
        caps.setCapability("appPackage", "com.cnezsoft.xxm");
        caps.setCapability("appActivity", ".activity.MainActivity");
        caps.setCapability("noReset", true);

        // 创建与 Appium Server 的连接
        AppiumDriver<MobileElement> driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), caps);

        // 设置全局等待时长
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


        // 定位到“创建聊天”按钮
        MobileElement newGroupBtn = driver.findElement(By.id("com.cnezsoft.xxm:id/chat_contact_create_discussion_group"));
        newGroupBtn.click();

        // 定位到“选择联系人”页面的“搜索框”，并输入关键词进行搜索
        MobileElement searchBox = driver.findElement(By.id("com.cnezsoft.xxm:id/addressbook_search_input"));
        searchBox.sendKeys("jia");

        // 定位到搜索结果中的第一个联系人并点击
        MobileElement firstContact = driver.findElement(By.id("com.cnezsoft.xxm:id/addressbook_list_item_name"));
        firstContact.click();

        // 定位到“创建”按钮并点击
        MobileElement confirmBtn = driver.findElement(By.id("com.cnezsoft.xxm:id/addressbook_contact_action"));
        confirmBtn.click();

        // 关闭 Appium Session
        driver.quit();
    }
}
